$(function() {
	var bussinessId = $("#identify").val();
	// alert(bussinessId);

	initPage(bussinessId);

});
var pageInfo={};
$("#selectAll").change(function() { 
	for (var i = 0; i < pageInfo.length; i++){
		 $("#"+pageInfo[i].id).prop("checked",true);
		}
	}); 
$("#cancelAll").change(function() { 
	for (var i = 0; i < pageInfo.length; i++){
		 $("#"+pageInfo[i].id).prop("checked",false);
		}
	}); 
function initPage(bussinessId) {
	$.ajax({
				url : window.rootPath
						+ '/ui/bussinessCheck/pageByTimeOnPhone?device=mobile&&identify='
						+ bussinessId,
				dataType : 'json',
				type : 'get',
				success : function(result) {
					 pageInfo = result.data.pageInfo;
					var pageHistoryInfo=result.data.pageHistoryInfo;
					if (result.code == 1) {
						// 构建待考核数据
						if(pageInfo.length==0){
							/*var  contenthtml =   '<li>'  
                          +'<h2 style="color:blue">暂无待考核项</h2>'  
                          + '</li>';  
                          $("#waitingchecklistul").append(contenthtml);
                          $('#waitingchecklistul').listview('refresh');*/
							
							var  contenthtml =   '<tr><td colspan="2">'  
				                  +'<h2 style="color:blue">暂无待考核记录</h2>'  
				                  + '</tr></td>';  
				                  $("#mywaitingCheckTable").append(contenthtml);
						}
						if(pageHistoryInfo.length==0){
							var  contenthtml =   '<li>'  
                          +'<h2 style="color:blue">暂无考核历史</h2>'  
                          + '</li>';  
                          $("#historychecklistul").append(contenthtml);
                          $('#historychecklistul').listview('refresh');
						}
						
						for (var i = 0; i < pageInfo.length; i++) {
							var purl=pageInfo[i].pictureAddr;
							if(purl==undefined||purl==""){
								purl="../img/nopicture.png";
							}else{
								purl=window.rootPath+'/'+purl;
							}
							var title=pageInfo[i].inspectCondition.title;
							var titleColor="";
							
							if(title=="优秀"){
								titleColor="green";
							}else if(title="良好"){
								titleColor="blue";
							}else if(title="及格"){
								titleColor="yellow";
							}else if(title="不及格"){
								titleColor="red";
							}
							/*var  contenthtml =   '<li>'+'<input type="checkbox" name="favcolor" id="'+pageInfo[i].id+'" value="red">选择</input> <a href="#waitingchecklistul">'  
                            +'<img width="80%" src="'+purl+'" />'  
                          +'<h2 style="color:'+titleColor+'">总体评价:'+title+'</h2>'  
                          +'<p style="color:blue">时间：'+moment(pageInfo[i].inspectDate).format('YYYY-MM-DD')+'</p>'  
                          + '</a>' + '</li>';  
                          $("#waitingchecklistul").append(contenthtml);
                          $('#waitingchecklistul').listview('refresh');*/
							
							var  contenthtml ='<tr><td><img width="30%" src="'+purl+'" /></td><td><input type="checkbox" name="favcolor" id="'+pageInfo[i].id+'" value="red">选择</input></td></tr>'  
							  +'<tr><td><p>巡检人:</p></td><td><p>'+pageInfo[i].user.name+'</p></td></tr>' 
			                  +'<tr><td><p>时间：</p></td><td><p style="color:blue">'+moment(pageInfo[i].inspectDate).format('YYYY-MM-DD')+'</p></td></tr>'
			                  +'<tr><td><p>总体评价:</p></td><td><p style="color:'+titleColor+'">'+title+'</p></td></tr>'
							  +'<tr><td></td></tr>';
			                  $("#mywaitingCheckTable").append(contenthtml);
							
						}
						//构建历史考核数据
						for (var i = 0; i < pageHistoryInfo.length; i++) {
							var purl=pageHistoryInfo[i].pictureAddr;
							if(purl==undefined||purl==""){
								purl="../img/nopicture.png";
							}else{
								purl=window.rootPath+'/'+purl;	
							}
							/*var title=pageHistoryInfo[i].inspectCondition.title;
							var titleColor="";
							
							if(title=="优秀"){
								titleColor="green";
							}else if(title="良好"){
								titleColor="blue";
							}else if(title="及格"){
								titleColor="yellow";
							}else if(title="不及格"){
								titleColor="red";
							}*/
							
							var  contenthtml =   '<li>'+' <a href="#sPanel" class="aclick" id="'+pageHistoryInfo[i].id+'">'  
                            +'<img width="80%" src="'+purl+'" />'  
                          +'<h2>考核人:'+pageHistoryInfo[i].user.name+'</h2>'
                          +'<h2>整改情况:'+pageHistoryInfo[i].checkCondition+'</h2>'  
                          +'<p style="color:blue">时间：'+moment(pageHistoryInfo[i].checkDate).format('YYYY-MM-DD')+'</p>'  
                          + '</a>' + '</li>';  
                          $("#historychecklistul").append(contenthtml);
                          $('#historychecklistul').listview('refresh');
						}
					}
				}
			});
};
$("body").on("click", ".aclick", function (){
	$("#myDetailTable tr:not(:first)").remove();
	$("#myhistoryTable tr:not(:first)").remove();
	var a=$(this).attr("id");
	$.ajax({
		url : window.rootPath
				+ '/ui/bussinessCheck/checkDetailInfo?device=mobile&&identify='
				+ a,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			var recordInfo=result.data.recordInfo;
			var detailInfo =result.data.detailInfo;
			var bussinessCheck=result.data.bussinessCheck;
			
			$("#checkDate").html(moment(bussinessCheck.checkDate).format('YYYY-MM-DD'));
			$("#user").html(bussinessCheck.user.name);
			$("#checkCondition").html(bussinessCheck.checkCondition);
			$("#rectifyCondition").html(bussinessCheck.rectifyCondition);
			$("#remark").html(bussinessCheck.remark);
			var imgUrl=bussinessCheck.pictureAddr;
			if(imgUrl==undefined||imgUrl==""){
				imgUrl="../img/nopicture.png";
			}else{
				imgUrl=window.rootPath+'/'+imgUrl;
			}
			$("#avatartD").attr("src",imgUrl);
			
			if (result.code == 1) {
				// 巡检历史
				if(recordInfo.length==0){
					var  contenthtml =   '<tr><td colspan="2">'  
                  +'<h2 style="color:blue">暂无巡检历史记录</h2>'  
                  + '</tr></td>';  
                  $("#myhistoryTable").append(contenthtml);
				}
				//遗留详情
				if(detailInfo.length==0){
					var  contenthtml =   '<tr><td colspan="2">'  
		                  +'<h2 style="color:blue">暂无巡检核查详情</h2>'  
		                  + '</tr></td>';  
		                  $("#myDetailTable").append(contenthtml);
				}
				
				for (var i = 0; i < recordInfo.length; i++) {
					var purl=recordInfo[i].pictureAddr;
					if(purl==undefined||purl==""){
						purl="../img/nopicture.png";
					}else{
						purl=window.rootPath+'/'+purl;
					}
					var title=recordInfo[i].inspectCondition.title;
					var titleColor="";
					
					if(title=="优秀"){
						titleColor="green";
					}else if(title="良好"){
						titleColor="blue";
					}else if(title="及格"){
						titleColor="yellow";
					}else if(title="不及格"){
						titleColor="red";
					}
					var  contenthtml ='<tr><td colspan="2"><img width="30%" src="'+purl+'" /></td></tr>'  
				  +'<tr><td><p>巡检人:</p></td><td><p>'+recordInfo[i].user.name+'</p></td></tr>' 
                  +'<tr><td><p>时间：</p></td><td><p style="color:blue">'+moment(recordInfo[i].inspectDate).format('YYYY-MM-DD')+'</p></td></tr>'
                  +'<tr><td><p>总体评价:</p></td><td><p style="color:'+titleColor+'">'+title+'</p></td></tr>'
				  +'<tr><td></td></tr>';
                  $("#myhistoryTable").append(contenthtml);
				}
				//构建历史考核数据
				for (var i = 0; i < detailInfo.length; i++) {
					var purl=detailInfo[i].pictureAdress;
					if(purl==undefined||purl==""){
						purl="../img/nopicture.png";
					}else{
						purl=window.rootPath+'/'+purl;
					}
					
					var  contenthtml ='<tr><td colspan="2"><img width="30%" src="'+purl+'"/></td></tr>'  
					  +'<tr><td><p>问题:</p></td><td><p>'+detailInfo[i].checkTypeInfo.title+'</p></td></tr>' 
					  +'<tr><td><p>是否整改:</p></td><td><p>'+detailInfo[i].repairInfo+'</p></td></tr>'
					  +'<tr><td><p>备注:</p></td><td><p color="gray">'+detailInfo[i].memo+'</p></td></tr>' 
	                  +'<tr><td><p>时间</p></td><td><p style="color:blue">'+moment(detailInfo[i].createTime).format('YYYY-MM-DD')+'</p></td></tr>'
	                  +'<tr><td></td></tr>';
	                  $("#myDetailTable").append(contenthtml);
				}
			}
		}
	});
	});
$(document).ready(function() {
	$("#goHome").attr("href",$("#goHome").attr("href")+"&&identify="+$('#identify').val());
});
// 删除该条巡检,使用的是restful风格的DELETE提交方式
function deleteInspection(data) {

	if (confirm('确定删除该条巡检记录?')) {
		$.ajax({
			url : window.rootPath + '/ui/bussinessCheck/delete/' + data.id,
			dataType : 'json',
			type : 'get',
			data : '_method=DELETE',
			success : function(result) {
				// 返回成功gg
				if (result.code == 1) {
					// 放回商家的巡检页面
					window.location = window.rootPath
							+ '/ui/bussinessCheck?device=mobile&identify='
							+ data.businesssellerBaseInfo.id;
				}
			}
		});
	}
};

// 修改页面
function editInspection(data) {
	if (confirm('确定修改该条巡检记录?')) {
		// 修改页面
		window.location = window.rootPath + "/ui/bussinessCheck/saveOrUpdate/"
				+ data.id + "?device=mobile&identify="
				+ data.businesssellerBaseInfo.id;
	}
};

$("#saveCheckBtn").click(
		function() {
			var inspectionIdList="";
			for (var i = 0; i < pageInfo.length; i++){
			 if($("#"+pageInfo[i].id).is(":checked")){
				 inspectionIdList+=pageInfo[i].id+";";
			 }
			}
			if(inspectionIdList!=""){
				window.location = window.rootPath
				+ "/ui/bussinessCheck/saveOrUpdate?device=mobile&identify="
				+ $("#identify").val()+"&inspectionIdList="+inspectionIdList;	
			}else{
				$("#alterpop").popup("open");
			}
		});
